package com.cssy.yd.logic.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.basic.dao.BasicCompanyDAO;
import com.cssy.yd.basic.entity.BasicCompanyDO;
import com.cssy.yd.logic.dao.LogicPoDAO;
import com.cssy.yd.logic.dao.LogicPoItemDAO;
import com.cssy.yd.logic.dao.LogicPoQuoteDAO;
import com.cssy.yd.logic.entity.LogicPoDO;
import com.cssy.yd.logic.entity.LogicPoItemDO;
import com.cssy.yd.logic.entity.LogicPoQuoteDO;
import com.cssy.yd.logic.entity.PoGatherExecuteReportFormsDO;
import com.cssy.yd.logic.entity.PoGatherReportFormsDO;
import com.cssy.yd.logic.entity.PoItemExecuteReportFormsDO;
import com.cssy.yd.logic.entity.PoItemReportFormsDO;
import com.cssy.yd.logic.service.ILogicPoService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;
@Service("logicPoService")
public class LogicPoServiceImpl implements ILogicPoService {
	
	@Resource(name = "logicPoDAO")
	LogicPoDAO logicPoDAO;
	
	@Resource(name = "logicPoItemDAO")
	LogicPoItemDAO logicPoItemDAO;
	
	@Resource(name="logicPoQuoteDAO")
	private LogicPoQuoteDAO	logicPoQuoteDAO;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name="basicCompanyDAO")	
	private BasicCompanyDAO basicCompanyDAO;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicPoDO>  list=logicPoDAO.find(scs);
		rs.setRecordCount(logicPoDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public List<LogicPoDO> findListByExample(LogicPoDO logicPoDO)
			throws SyBaseException {
		
		return logicPoDAO.findListByExample(logicPoDO);
	}
	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		logicPoDAO.deleteLogicPoDOByPrimaryKey(id);
		return null;
	}

	@Override
	public Integer update(LogicPoDO t, User user) throws SyBaseException {
		t.setVersion(t.getVersion()+1);
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		logicPoDAO.updateLogicPoDO(t);
		return null;
	}

	@Override
	public LogicPoDO save(LogicPoDO t, User user) throws SyBaseException {
		t.setVersion(Double.parseDouble("1"));
		t.setDcruser(user.getName());
		t.setDctime(new Date());
		t.setId(IDCodeTypeUtil.getId());
		logicPoDAO.insertLogicPoDO(t);
		return t;
	}

	@Override
	public LogicPoDO findListById(String id) throws SyBaseException {
		return logicPoDAO.findLogicPoDOByPrimaryKey(id);
	}

	@Override
	public void poSave(HttpServletRequest request, LogicPoDO curAP)
			throws SyBaseException {
		try {
			String bhid  =request.getParameter("id");
			String itemdata =request.getParameter("itemdata");
			String itemdelete =request.getParameter("itemdelete");
			String dgrq=request.getParameter("dgrq");
			String shrq=request.getParameter("shrq");
			String fkdate=request.getParameter("fkdate");
			String type=request.getParameter("type");
			curAP.setDgrq(DateUtils.stringToDate(dgrq));
			curAP.setShrq(DateUtils.stringToDate(shrq));
			curAP.setFkdate(DateUtils.stringToDate(fkdate));
			List<LogicPoItemDO> lp = JSONArray.parseArray(itemdata,
					LogicPoItemDO.class);//LogicPoApplyItemDO
			if("bh".equals(type)){
				LogicPoDO logicPoDO = logicPoDAO.findLogicPoDOByPrimaryKey(bhid);
				logicPoDO.setBillstatus("0");
				this.update(logicPoDO,  SessionUtil.getUser(request));
			}else{
					if("0".equals(type) || "1".equals(type)){
						if(!"".equals(type)&&type!=null){
							curAP.setBillstatus(type);
						}
						if (curAP.getId() != null && !"".equals(curAP.getId())) {// 修改
							curAP.setVersion(curAP.getVersion()+1);
							curAP.setDuuser(SessionUtil.getUser(request).getName());
							curAP.setDutime(new Date());
							int result =logicPoDAO.updateLogicPoDO(curAP);
							if(1==result){
								if (itemdelete != null && !"".equals(itemdelete)) {
									List<LogicPoItemDO> lpiem = JSONArray.parseArray(itemdelete, LogicPoItemDO.class);
									for (int i = 0; i < lpiem.size(); i++) {
										if (lpiem.get(i).getId() != null&& !"".equals(lpiem.get(i).getId())) {
											logicPoItemDAO.deleteLogicPoItemDOByPrimaryKey(lpiem.get(i).getId());
										}
									}
								}
								for (int i = 0; i < lp.size(); i++) {
									lp.get(i).setPoId(curAP.getId());
									lp.get(i).setMaterialsid(lp.get(i).getMaterialsid());
									if (lp.get(i).getId() != null&& !"".equals(lp.get(i).getId())) {
										logicPoItemDAO.updateLogicPoItemDO(lp.get(i));
									} else {
										lp.get(i).setId(IDCodeTypeUtil.getId());
										logicPoItemDAO.insertLogicPoItemDO(lp.get(i));
									}
								}
							}else{
								throw new SyBaseException("您修改的行数据发生改变，请重新点开查看");
							}
						} else {
							String id = IDCodeTypeUtil.getId();
							curAP.setPocode(syCodeService.getCode("PO","PO",SyCode.DATE_YYMM,5));
							curAP.setDcruser(SessionUtil.getUser(request).getName());
							curAP.setDctime(new Date());
							curAP.setId(id);
							curAP.setVersion(Double.parseDouble("1"));
//							curAP.setDcruser(SessionUtil.getUser(request).getName());
//							curAP.setDctime(new Date());
							logicPoDAO.insertLogicPoDO(curAP);
							if (lp.size() > 0){
								for (int i = 0;i < lp.size();i++){
									lp.get(i).setId(IDCodeTypeUtil.getId());
									lp.get(i).setPoId(id);
									lp.get(i).setDctime(new Date());
									lp.get(i).setDcruser(SessionUtil.getUser(request).getName());
									logicPoItemDAO.insertLogicPoItemDO(lp.get(i));
								}
							}
						}
					}else{
						if(!"".equals(curAP.getId()) && curAP.getId() != null){
							LogicPoDO po =  this.findListById(curAP.getId());
							if(Integer.parseInt(po.getBillstatus())>=5){
								throw new SyBaseException("该采购订单已经完结!");
							}
						}
						LogicPoDO lDo = new LogicPoDO();
						lDo.setId(curAP.getId());
						lDo.setBillstatus(type);
						logicPoDAO.updateLogicPoDO(lDo);
					}
//					if("3".equals(type)){
//						SearchConditions scs2 = new SearchConditions();
//					    scs2.addCondition("name", curAP.getSpname(),SearchCondition.OP_EQUAL);
//					    scs2.addCondition("parentname", "供应商",SearchCondition.OP_EQUAL);
//					    List<BasicCompanyDO> baCompanyDAOs =basicCompanyDAO.find(scs2);
//					    LogicPoQuoteDO lDo = new LogicPoQuoteDO();
//					    lDo.setGysid(baCompanyDAOs.get(0).getId());
//					    lDo.setGysName(baCompanyDAOs.get(0).getName());
//					    lDo.setGysAddress(baCompanyDAOs.get(0).getAddress());
//					    lDo.setGysLinkman(baCompanyDAOs.get(0).getAttachperson());
//					    lDo.setGysPhone(baCompanyDAOs.get(0).getMobil());
//					    lDo.setGysTel(baCompanyDAOs.get(0).getTelephone());
//					    lDo.setFax(baCompanyDAOs.get(0).getFax());
//					    lDo.setFoundPerson(SessionUtil.getUser(request).getName());
//					    lDo.setBillstatus(3);
//					    lDo.setDcruser(SessionUtil.getUser(request).getName());
//					    lDo.setDctime(new Date());
//					    lDo.setId(IDCodeTypeUtil.getId());
//					    logicPoQuoteDAO.insertLogicPoQuoteDO(lDo);
//						for (LogicPoItemDO logicPoItemDO : lp) {
//							SearchConditions scs = new SearchConditions();
//						    scs.addCondition("gys_name", curAP.getSpname(),SearchCondition.OP_EQUAL);
//						    scs.addCondition("materialsname", logicPoItemDO.getName(),SearchCondition.OP_EQUAL);
//						    scs.addCondition("price", ((Integer)logicPoItemDO.getPrice().intValue()).toString(),SearchCondition.OP_EQUAL);
//							List<LogicPoQuoteDO> lQuoteDO =logicPoQuoteDAO.find(scs);
//							if(lQuoteDO.size() <= 0){
//								
////							    lDo.setGoodsModel(logicPoItemDO.getSpec());
////							    lDo.setMaterialsid(logicPoItemDO.getMaterialsid());
////							    lDo.setCode(logicPoItemDO.getCode());
////							    lDo.setMaterialsname(logicPoItemDO.getName());
////							    lDo.setMaterialstype(logicPoItemDO.getItmetype());
////							    lDo.setMaterialsunit(logicPoItemDO.getJunit());
////							    lDo.setTaxPrice(logicPoItemDO.getStockPrice());
////							    lDo.setDiscount(logicPoItemDO.getDiscount());
////							    lDo.setPrice(logicPoItemDO.getPrice());
////							    lDo.setTaxPriceCount(logicPoItemDO.getSumPMoney());
//							    
//							}
//						}
//					}
//					if("5".equals("type")){
//						LogicPoDO lDo = new LogicPoDO();
//						lDo.setId(curAP.getId());
//						lDo.setBillstatus(type);
//						logicPoDAO.updateLogicPoDO(lDo);
//					}
				}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}

	@Override
	public SearchResult findPoGatherByExample(Map<String, Object> map)
			throws SyBaseException {
		SearchResult rs=new SearchResult();
		List<PoGatherReportFormsDO>  list=logicPoDAO.findPoGather(map);
		PoGatherReportFormsDO pFormsDO = new PoGatherReportFormsDO();
		pFormsDO.setWlCode("合计");
		for (PoGatherReportFormsDO poGatherReportFormsDO : list) {
			pFormsDO.setRkCountJquantity((pFormsDO.getRkCountJquantity()==null?0.0:pFormsDO.getRkCountJquantity())+poGatherReportFormsDO.getRkCountJquantity());
			pFormsDO.setRkCountQuantity((pFormsDO.getRkCountQuantity()==null?0.0:pFormsDO.getRkCountQuantity())+poGatherReportFormsDO.getRkCountQuantity());
			pFormsDO.setPrice((pFormsDO.getPrice()==null?0.0:pFormsDO.getPrice())+poGatherReportFormsDO.getPrice());
			pFormsDO.setSumMoney((pFormsDO.getSumMoney()==null?0.0:pFormsDO.getSumMoney())+poGatherReportFormsDO.getSumMoney());
		}
		list.add(pFormsDO);
		rs.setRecordCount(logicPoDAO.findPoGatherCount(map));
		rs.setResults(list);
		return rs;
	}

	@Override
	public SearchResult findPoItemByExample(Map<String, Object> map)
			throws SyBaseException {
		SearchResult rs=new SearchResult();
		List<PoItemReportFormsDO>  list=logicPoDAO.findPoItem(map);
		PoItemReportFormsDO pItemReportFormsDO = new PoItemReportFormsDO();
		pItemReportFormsDO.setDjCode("合计");
		for (PoItemReportFormsDO poItemReportFormsDO : list) {
			pItemReportFormsDO.setDdJquantity((pItemReportFormsDO.getDdJquantity()==null?0.0:pItemReportFormsDO.getDdJquantity())+poItemReportFormsDO.getDdJquantity());
			pItemReportFormsDO.setDdQuantity((pItemReportFormsDO.getDdQuantity()==null?0.0:pItemReportFormsDO.getDdQuantity())+poItemReportFormsDO.getDdQuantity());
//			pItemReportFormsDO.setBhsPrice((pItemReportFormsDO.getBhsPrice()==null?0.0:pItemReportFormsDO.getBhsPrice())+poItemReportFormsDO.getBhsPrice());
//			pItemReportFormsDO.setBhsSumMonery((pItemReportFormsDO.getBhsSumMonery()==null?0.0:pItemReportFormsDO.getBhsSumMonery())+poItemReportFormsDO.getBhsSumMonery());
			pItemReportFormsDO.setRkJquantity((pItemReportFormsDO.getRkJquantity()==null?0.0:pItemReportFormsDO.getRkJquantity())+poItemReportFormsDO.getRkJquantity());
			pItemReportFormsDO.setRkQuantity((pItemReportFormsDO.getRkQuantity()==null?0.0:pItemReportFormsDO.getRkQuantity())+poItemReportFormsDO.getRkQuantity());
			pItemReportFormsDO.setRkPrice((pItemReportFormsDO.getRkPrice()==null?0.0:pItemReportFormsDO.getRkPrice())+poItemReportFormsDO.getRkPrice());
			pItemReportFormsDO.setRkSumMonery((pItemReportFormsDO.getRkSumMonery()==null?0.0:pItemReportFormsDO.getRkSumMonery())+poItemReportFormsDO.getRkSumMonery());
		}
		list.add(pItemReportFormsDO);
		rs.setRecordCount(logicPoDAO.findPoItemCount(map));
		rs.setResults(list);
		return rs;
	}

	@Override
	public SearchResult findPoGatherExecuteByExample(Map<String, Object> map)
			throws SyBaseException {
		SearchResult rs=new SearchResult();
		List<PoGatherExecuteReportFormsDO>  list=logicPoDAO.findPoGatherExecute(map);
		PoGatherExecuteReportFormsDO pExecuteReportFormsDO = new PoGatherExecuteReportFormsDO();
		pExecuteReportFormsDO.setWlCode("合计");
		for (PoGatherExecuteReportFormsDO poGatherExecuteReportFormsDO : list) {
			pExecuteReportFormsDO.setDdJquantity((pExecuteReportFormsDO.getDdJquantity()==null?0.0:pExecuteReportFormsDO.getDdJquantity())+poGatherExecuteReportFormsDO.getDdJquantity());
			pExecuteReportFormsDO.setDdQuantity((pExecuteReportFormsDO.getDdQuantity()==null?0.0:pExecuteReportFormsDO.getDdQuantity())+poGatherExecuteReportFormsDO.getDdQuantity());
//			pExecuteReportFormsDO.setDdPrice((pExecuteReportFormsDO.getDdPrice()==null?0.0:pExecuteReportFormsDO.getDdPrice())+poGatherExecuteReportFormsDO.getDdPrice());
//			pExecuteReportFormsDO.setSumMoney((pExecuteReportFormsDO.getSumMoney()==null?0.0:pExecuteReportFormsDO.getSumMoney())+poGatherExecuteReportFormsDO.getSumMoney());
			pExecuteReportFormsDO.setRkJquantity((pExecuteReportFormsDO.getRkJquantity()==null?0.0:pExecuteReportFormsDO.getRkJquantity())+poGatherExecuteReportFormsDO.getRkJquantity());
			pExecuteReportFormsDO.setRkQuantity((pExecuteReportFormsDO.getRkQuantity()==null?0.0:pExecuteReportFormsDO.getRkQuantity())+poGatherExecuteReportFormsDO.getRkQuantity());
			pExecuteReportFormsDO.setWrkJquantity((pExecuteReportFormsDO.getWrkJquantity()==null?0.0:pExecuteReportFormsDO.getWrkJquantity())+poGatherExecuteReportFormsDO.getWrkJquantity());
			pExecuteReportFormsDO.setWrkQuantity((pExecuteReportFormsDO.getWrkQuantity()==null?0.0:pExecuteReportFormsDO.getWrkQuantity())+poGatherExecuteReportFormsDO.getWrkQuantity());
		}
		list.add(pExecuteReportFormsDO);
		rs.setRecordCount(logicPoDAO.findPoGatherExecuteCount(map));
		rs.setResults(list);
		return rs;
	}

	@Override
	public SearchResult findPoItemExecuteByExample(Map<String, Object> map)
			throws SyBaseException {
		SearchResult rs=new SearchResult();
		List<PoItemExecuteReportFormsDO>  list=logicPoDAO.findPoItemExecute(map);
		PoItemExecuteReportFormsDO pReportFormsDO = new PoItemExecuteReportFormsDO();
		pReportFormsDO.setDdCode("合计");
		for (PoItemExecuteReportFormsDO poItemExecuteReportFormsDO : list) {
			pReportFormsDO.setDdJquantity((pReportFormsDO.getDdJquantity()==null?0.0:pReportFormsDO.getDdJquantity())+poItemExecuteReportFormsDO.getDdJquantity());
			pReportFormsDO.setDdQuantity((pReportFormsDO.getDdQuantity()==null?0.0:pReportFormsDO.getDdQuantity())+poItemExecuteReportFormsDO.getDdQuantity());
			pReportFormsDO.setRkJquantity((pReportFormsDO.getRkJquantity()==null?0.0:pReportFormsDO.getRkJquantity())+poItemExecuteReportFormsDO.getRkJquantity());
			pReportFormsDO.setRkQuantity((pReportFormsDO.getRkQuantity()==null?0.0:pReportFormsDO.getRkQuantity())+poItemExecuteReportFormsDO.getRkQuantity());
			pReportFormsDO.setWrkJquantity((pReportFormsDO.getWrkJquantity()==null?0.0:pReportFormsDO.getWrkJquantity())+poItemExecuteReportFormsDO.getWrkJquantity());
			pReportFormsDO.setWrkQuantity((pReportFormsDO.getWrkQuantity()==null?0.0:pReportFormsDO.getWrkQuantity())+poItemExecuteReportFormsDO.getWrkQuantity());
		}
		list.add(pReportFormsDO);
		rs.setRecordCount(logicPoDAO.findPoItemExecuteCount(map));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}


//	@Override
//	public SearchResult findByExample(Map scs)
//			throws SyBaseException {
//		SearchResult rs=new SearchResult();
//		List<LogicPoVO>  list=logicPoDAO.findAll(scs);
//		rs.setRecordCount(logicPoDAO.findCount(scs));
//		rs.setResults(list);
//		return rs;
//	}

}
